# -*- coding: utf-8 -*-
# @author: edgardeng
# @date:   2021-05-08
# @file:   [websocket] https://websockets.readthedocs.io/en/stable/
import asyncio
import time
import sys
import websockets

now = lambda: time.time()


async def get_channel_status(channel):
    uri = "ws://localhost:8001/channel_live_status"
    try:
        async with websockets.connect(uri) as websocket:
            await websocket.send(channel)
            data = await websocket.recv()
            print(data)
            while not websocket.closed:
                data = await websocket.recv()
                print(data)
    except Exception as e:
        print(e)
    print('end')


if __name__ == '__main__':
    user = str(sys.argv[1]) if len(sys.argv) >= 2 else 'none'

    t0 = time.time()
    print('start', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(t0)))
    loop = asyncio.get_event_loop()
    # task = asyncio.ensure_future(sanic_channel_status())
    loop.run_until_complete(get_channel_status(user))  # 单任务

    t1 = time.time()
    print('end', time.strftime('%Y-%M-%D %H:%M:%S', time.localtime(t1)), t1 - t0)
